//
//  ChangePriceCell.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/12/1.
//  Copyright © 2018 小河. All rights reserved.
//

#import "ChangePriceCell.h"

@implementation ChangePriceCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setMainCell];
    }
    return self;
}

- (void)setMainCell
{
    self.topTitleLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.topTitleLabel];
    self.topTitleLabel.textAlignment = 0;
    self.topTitleLabel.font = FontWithSize(14);
    self.topTitleLabel.textColor = RGBACOLOR(17, 17, 17, 1);
    self.topTitleLabel.numberOfLines = 0;
    self.topTitleLabel.userInteractionEnabled = YES;
    
    self.topTitleLabelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.topTitleLabel addSubview:self.topTitleLabelBtn];
    
    self.recommendLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.recommendLabel];
    self.recommendLabel.textAlignment = 0;
    self.recommendLabel.font = FontWithSize(12);
    self.recommendLabel.textColor = RGBACOLOR(239, 208, 83, 1);
    
    self.costLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.costLabel];
    self.costLabel.font = FontWithSize(12);
    self.costLabel.textColor = RGBACOLOR(194, 194, 194, 1);
    self.costLabel.textAlignment = 2;
    
    self.priceBgView = [[UIView alloc] init];
    [self.contentView addSubview:self.priceBgView];
    self.priceBgView.backgroundColor = GrayTextColor;
    
    self.priceLabel = [[UILabel alloc] init];
    [self.priceBgView addSubview:self.priceLabel];
    self.priceLabel.textColor = BlackTextColor;
    self.priceLabel.font = FontWithSize(12);
    
    self.earningsLabel = [[UILabel alloc] init];
    [self.priceBgView addSubview:self.earningsLabel];
    self.earningsLabel.textColor = BlackTextColor;
    self.earningsLabel.font = FontWithSize(12);
    self.earningsLabel.textAlignment = 2;
    
    self.leftImage = [[UIImageView alloc]init];
    [self.contentView addSubview:self.leftImage];
    self.leftImage.contentMode = UIViewContentModeScaleAspectFill;
    self.leftImage.clipsToBounds = YES;
    self.leftImage.userInteractionEnabled = YES;
    self.leftImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.leftImage addSubview:self.leftImageBtn];
    
    self.rightTopImgae = [[UIImageView alloc]init];
    [self.contentView addSubview:self.rightTopImgae];
    self.rightTopImgae.contentMode = UIViewContentModeScaleAspectFill;
    self.rightTopImgae.clipsToBounds = YES;
    self.rightTopImgae.userInteractionEnabled = YES;
    self.rightImgaeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightTopImgae addSubview:self.rightImgaeBtn];
    
    self.timeLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.timeLabel];
    self.timeLabel.textAlignment = 0;
    self.timeLabel.font = FontWithSize(12);
    self.timeLabel.textColor = RGBACOLOR(66, 66, 66, 1);
    
    self.shanrkBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:self.shanrkBtn];
    [self.shanrkBtn setTitle:@"分享" forState:UIControlStateNormal];
    [self.shanrkBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
    self.shanrkBtn.titleLabel.font = FontWithSize(12);
    self.shanrkBtn.backgroundColor = BtnBgColor;
    self.shanrkBtn.layer.cornerRadius = 15;
    self.shanrkBtn.layer.masksToBounds = YES;
    
    self.changePriceBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:self.changePriceBtn];
    [self.changePriceBtn setTitle:@"改价" forState:UIControlStateNormal];
    [self.changePriceBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
    self.changePriceBtn.titleLabel.font = FontWithSize(12);
    self.changePriceBtn.backgroundColor = [UIColor whiteColor];
    self.changePriceBtn.layer.cornerRadius = 15;
    self.changePriceBtn.layer.masksToBounds = YES;
    self.changePriceBtn.layer.borderWidth = 1;
    self.changePriceBtn.layer.borderColor = BtnBgColor.CGColor;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.topTitleLabel.frame = CGRectMake(15, 20, self.width-30, 40);
    self.topTitleLabelBtn.frame = CGRectMake(0, 0, self.topTitleLabel.width, self.topTitleLabel.height);
    
    self.recommendLabel.frame = CGRectMake(15, self.topTitleLabel.bottom+8, SCREENWIDTH-30, 12);
    self.costLabel.frame = CGRectMake(SCREENWIDTH-25-200, self.recommendLabel.bottom+8, 200, 12);
    self.priceBgView.frame = CGRectMake(15, self.costLabel.bottom+10, self.topTitleLabel.width, 34);
    self.priceLabel.frame = CGRectMake(10, 11, 150, 12);
    self.earningsLabel.frame = CGRectMake(self.priceBgView.width-150-10, 11, 150, 12);
    
    CGFloat imageWidth = (SCREENWIDTH-34)/2;
    self.leftImage.frame = CGRectMake(15, self.priceBgView.bottom+10, imageWidth, imageWidth);
    
    self.rightTopImgae.frame = CGRectMake(self.leftImage.right+4, self.leftImage.top, imageWidth, imageWidth);
    
    self.leftImageBtn.frame =CGRectMake(0, 0, self.leftImage.width, self.leftImage.height);
    self.rightImgaeBtn.frame =CGRectMake(0, 0, self.leftImage.width, self.leftImage.height);
    
    self.timeLabel.frame = CGRectMake(15, self.leftImage.bottom+41, 130, 12);
    self.shanrkBtn.frame = CGRectMake(self.width-70-15, self.leftImage.bottom+20, 70, 30);
    self.changePriceBtn.frame = CGRectMake(self.shanrkBtn.left-70-10, self.leftImage.bottom+20, 70, 30);
}

- (void)setCellModel:(MainModel *)model
{
    self.topTitleLabel.text = [NSString stringWithFormat:@"%@",model.title];
    if ([model.priceChangeType isEqualToString:@"2"])
    {
        self.recommendLabel.text = @"该商品由于成本价变动，售价已加上变动价";
    }
    self.costLabel.text = [NSString stringWithFormat:@"成本价：￥%0.2f元",[model.shopCostPrice floatValue]];
    
    self.priceLabel.text = [NSString stringWithFormat:@"店主销售价：￥%0.2f", [model.price floatValue]];
    
    CGFloat earnings = [model.price floatValue] - [model.shopCostPrice floatValue];
    self.earningsLabel.text = [NSString stringWithFormat:@"预计收益：￥%0.2f",earnings];
    self.leftImage.hidden = YES;
    self.rightTopImgae.hidden = YES;
    for (int i = 0; i< model.imageUrlList.count; i++)
    {
        if (i == 0)
        {
            self.leftImage.hidden = NO;
            [self.leftImage sd_setImageWithURL:[NSURL URLWithString:model.imageUrlList[i]] placeholderImage:[UIImage imageNamed:@"zhanwei400"]];
        }
        else if (i == 1)
        {
            self.rightTopImgae.hidden = NO;
            [self.rightTopImgae sd_setImageWithURL:[NSURL URLWithString:model.imageUrlList[i]] placeholderImage:[UIImage imageNamed:@"zhanwei400"]];
        }
        
        if ([model.state integerValue] == 0)
        {//state商品状态，0： 下架， 1：正常
            [self.leftImageBtn setBackgroundImage:[UIImage imageNamed:@"soldout"] forState:UIControlStateNormal];
            [self.rightImgaeBtn setBackgroundImage:[UIImage imageNamed:@"soldout"] forState:UIControlStateNormal];
            self.changePriceBtn.hidden = YES;
            [self.shanrkBtn setTitle:@"已下架" forState:UIControlStateNormal];
            [self.shanrkBtn setTitleColor:RGBACOLOR(2, 2, 0, 1) forState:UIControlStateNormal];
            self.shanrkBtn.backgroundColor = RGBACOLOR(209, 209, 209, 1);
            self.shanrkBtn.userInteractionEnabled = NO;
        }
        else {
            [self.leftImageBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
            [self.rightImgaeBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
            self.changePriceBtn.hidden = NO;
            [self.shanrkBtn setTitle:@"分享" forState:UIControlStateNormal];
            [self.shanrkBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
            self.shanrkBtn.backgroundColor = BtnBgColor;
            self.shanrkBtn.userInteractionEnabled = YES;
        }
        
    }
    
    self.timeLabel.text = model.gmtModify;
}

@end
